package com.framework
{
	import com.eco.GameStage;
	
	import com.framework.*;
	import com.framework.SystemPage.AnimationPage;
	import com.framework.SystemPage.PagesManager;
	import com.framework.SystemPage.CoverPage;
	import com.framework.SystemPage.GamePage;
	import com.framework.SystemPage.PausePage;
	import com.framework.SystemPage.WinPage;
	import com.framework.SystemPage.HelpPage;
	import com.framework.SystemPage.LosePage;
	
	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.events.*;
	
	public class GameFrameWork extends Sprite
	{
		//Game Pause
		public var isPaused:Boolean = false;
		
		//System State Environment
		public var systemFunction:Function;
		public var currSystemState:int;
		public var prevSystemState:int;
		public var nextSystemState:int;					
		
		
		public var timerPeriod:Number; 					//每帧时间间隔
		public var gameTimer:Timer;						//游戏时钟
		public var frameRate = 30;						//默认帧速
		
		//menu pages
		
		public var pagesManeger:PagesManager;
		
		public var coverPage:BasicPage;
		public var mainPage:BasicPage;
		public var stageModePage:BasicPage;
		public var surviveModePage:BasicPage;
		public var helpPage:HelpPage;
		
		//page control varible
		public var currPage:BasicPage = null;
		public var prevPage:BasicPage = null;
		
		//public var optionPage:BasicPage;
		public var scoreBoardPage:BasicPage;
		
		
		//ingame pages
		public var pausePage:PausePage;
		public var winPage:WinPage;
		public var losePage:LosePage;
		public var animationPage:AnimationPage;
		
		public var gamePage:GamePage;
		public var showPage:BasicPage;
		
		//loading pages
		public var loadingPage:BasicPage;
		public var preloadingPage:BasicPage;
		public var preloaderStarted:Boolean=false;
		public var preloadString:String=new String();
		
		//time
		public var lastTime:Number;
		public var timeDifference:Number
		
		//xml data
		public var GameData:GameDataController = GameDataController.getInstance();
		
		//sound manager
		public var soundManager:SoundManager;
		
		//stage info
		protected var stage_id:int;
		protected var level_id:int;
		
		public function GameFrameWork()
		{
			super();
			soundManager = new SoundManager();
		}
		
		public function addToStage(e:Event=null):void
		{
			stage.focus = stage;		//焦点相应
			
		}
		
		public function init():void
		{
			//initialize system pages
			trace("inner initialization...");
			pagesManeger = new PagesManager;
			preloadingPage = pagesManeger.createPage(PageInfo.PAGE_PRELOADING);
			coverPage = pagesManeger.createPage(PageInfo.PAGE_COVER);
			mainPage = pagesManeger.createPage(PageInfo.PAGE_MAIN_2);
			stageModePage = pagesManeger.createPage(PageInfo.PAGE_STAGE_MODE);
			
			gamePage = new GamePage;
			
		}
		
		
		//function for system states switching
		public function startTimer(timeBasedAnimation:Boolean=false):void 
		{
			stage.frameRate = frameRate;
			
			if (timeBasedAnimation) {
				lastTime = getTimer();
				addEventListener(Event.ENTER_FRAME, runGameEnterFrame);
			}else{
				timerPeriod = 1000 / frameRate;
				gameTimer=new Timer(timerPeriod);
				gameTimer.addEventListener(TimerEvent.TIMER, runGame);
				gameTimer.start();
			}
		}
		
		public function runGame(e:TimerEvent):void 
		{
			systemFunction(); 	
		}
		
		public function runGameEnterFrame(e:Event):void 
		{
			timeDifference = getTimer() - lastTime
			lastTime = getTimer();
			systemFunction(); 	
		}
		
		public function switchSystemState(state_val:int)
		{
			prevSystemState = currSystemState;
			currSystemState = state_val;
			
			trace("current system state" + currSystemState);
			switch(state_val)
			{
				case FrameWorkStates.STATE_SYSTEM_COVER:
					systemFunction = systemCover;
					break;
				case FrameWorkStates.STATE_SYSTEM_MAIN:
					systemFunction = systemMain;
					break;
				case FrameWorkStates.STATE_SYSTEM_GAME_IN:
					systemFunction = systemGame;
					break;
				case FrameWorkStates.STATE_SYSTEM_GAME:
					systemFunction = systemGame;
					break;
				case FrameWorkStates.STATE_SYSTEM_PAUSE:
					systemFunction = systemPause;
					break;
				case FrameWorkStates.STATE_SYSTEM_NEW_GAME:
					systemFunction = systemNewGame;
					break;
				case FrameWorkStates.STATE_SYSTEM_HELP:
					systemFunction = systemHelp;
					break;
				case FrameWorkStates.STATE_STAGE_MODE:
					systemFunction = systemStageMode;
					break;
				case FrameWorkStates.STATE_SYSTEM_LOADING:
					systemFunction = systemPreloading;
					break;
				case FrameWorkStates.STATE_SYSTEM_WIN:
					systemFunction = systemWin; 
					break;
				case FrameWorkStates.STATE_SYSTEM_LOSE:
					systemFunction = systemLose; 
					break;
				case FrameWorkStates.STATE_SYSTEM_END:
					systemFunction = systemEnd; 
					break;
				case FrameWorkStates.STATE_SYSTEM_ANIMATION:
					systemFunction = systemAnimation;
					break;
			}
			
		}
		
		public function buttonEventListener(e:CustomEventButton):void
		{
			switch(e.name)
			{
				case ButtonInfo.BUTTON_BACK:
				{
					switchSystemState(prevSystemState);
				}
					break;
				case ButtonInfo.BUTTON_HELP:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_HELP);
				}
					break;
				case ButtonInfo.BUTTON_PLAY:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_MAIN);
				}
					break;
				case ButtonInfo.BUTTON_COVER:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_COVER);
				}
					break;
				case ButtonInfo.BUTTON_STAGE_MODE:
				{
					switchSystemState(FrameWorkStates.STATE_STAGE_MODE);
				}
					break;
				case ButtonInfo.BUTTON_MAIN:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_MAIN);
				}
					break;
				case ButtonInfo.BUTTON_STAGE_BEGIN:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_GAME);
				}
					break;
				case ButtonInfo.BUTTON_PAUSE:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_PAUSE);
				}
					break;
				case ButtonInfo.BUTTON_REPLAY:
				{
					stage_id = gamePage.stage_id;
					level_id = gamePage.level_id;
					dispatchEvent(new CustomEventButton(CustomEventButton.STAGE_ID,ButtonInfo.BUTTON_REPLAY,0,stage_id,level_id));
				}
					break;
				
				//按钮进入下一个场景
				case ButtonInfo.BUTTON_NEXT_STAGE:
				{
					trace(GameDataController.getInstance().LevelInfoArray);
					
					stage_id = gamePage.stage_id;
					level_id = gamePage.level_id;
					if(stage_id<=GameData.LevelInfoArray.length+1)
					{
						level_id+=1;//挪至下一关
						//如果关卡数大于总关数
						if(level_id>GameData.LevelInfoArray[stage_id-1]+1)
						{
							level_id = 1;
							stage_id += 1;
						}
					}
					trace("STAGE_ID: ",stage_id,"|| ",level_id)
					dispatchEvent(new CustomEventButton(CustomEventButton.STAGE_ID,ButtonInfo.BUTTON_NEXT_STAGE,0,stage_id,level_id));
				}
					break;
				
				case ButtonInfo.BUTTON_ANIMATION:
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_ANIMATION);
				}
					break;
			}
		}
		
		//关卡选择侦听
		public function stageEventListener(e:CustomEventButton):void
		{
			trace("LISTENED");
			gamePage.myStageManager.EndCurrentStage();	
			gamePage = null;
			//必须先清空再new新的游戏页面 否则不会生成  必须置为null 回收内存 
			if(gamePage == null)
			{
				gamePage = new GamePage();
			}
			gamePage.setStartStageListener(e);
			switchSystemState(FrameWorkStates.STATE_SYSTEM_GAME);
			
		}
		
		//游戏内状态侦听 输赢页面
		public function gameEventListener(e:CustomEventResult):void
		{
			switch(e.type)
			{
				case CustomEventResult.LEVEL_LOSE:
					switchSystemState(FrameWorkStates.STATE_SYSTEM_LOSE);
					break;
				case CustomEventResult.LEVEL_WIN:
					switchSystemState(FrameWorkStates.STATE_SYSTEM_WIN);
					break;
			}
			
		}
		
		//载入页面
		private function systemPreloading():void
		{
			if(!preloaderStarted)
			{
				//display preloading page
				addChild(preloadingPage);
				preloaderStarted=true;
			}
			else
			{
				var percentloaded:int = (this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)*100;
				preloadString = "loading:"+percentloaded+"%";
				trace(preloadString);
				
				exchangePrevPage(preloadingPage);
				removePrePage();
				
				if(percentloaded == 100)
				{
					switchSystemState(FrameWorkStates.STATE_SYSTEM_COVER);
				}
			}
			
			exchangePrevPage(preloadingPage);
			removePrePage();
		}
	
		public function systemCover():void
		{
			this.addChild(coverPage);
			exchangePrevPage(coverPage);
			removePrePage();
		}
		
		public function systemMain():void
		{
			this.addChild(mainPage);
			exchangePrevPage(mainPage);
			removePrePage();
		}
		
		public function systemStageMode():void
		{
			this.addChild(stageModePage);
			if(gamePage == null)
			{
				gamePage = new GamePage();
			}
			exchangePrevPage(stageModePage);			
			if(prevPage != null)
			{
				if(prevPage == gamePage || prevPage == pausePage || prevPage == winPage || prevPage == losePage)
				{
				
					gamePage.myStageManager.Clear();
					gamePage = null;
				}
				this.removeChild(prevPage);
				prevPage = null;
			}
		}
		
		public function systemGame():void
		{
			this.stage.focus = stage;					//设置键盘焦点使相应	
			if(gamePage==null)
			{
				gamePage = new GamePage;
				
			}
		//	gamePage.myStageManager.isPaused = false;
			gamePage.myStageManager.Continue();			
			this.addChild(gamePage);
			exchangePrevPage(gamePage);					//交换页面；把当前页面指针放入前一页指针
			removePrePage();							//移除前一页
		}
		
		public function systemPause():void
		{
			if(null== pausePage)
			{
				pausePage = new PausePage;
			}
			this.addChild(pausePage);
			gamePage.myStageManager.Pause();
			exchangePrevPage(pausePage);
		}
		
		//replay   没用 
		public function systemNewGame():void
		{
			gamePage.myStageManager.Continue();
			gamePage.myStageManager.RestartStage();
		//	switchSystemState(FrameWorkStates.STATE_SYSTEM_GAME);
			exchangePrevPage(gamePage);
			removePrePage();
		}
		
		
		public function systemWin():void
		{
			if(null== winPage)
			{
				winPage = new WinPage;
			}
			addChild(winPage);
			gamePage.myStageManager.Pause();
			exchangePrevPage(winPage);
		}
		
		public function systemLose():void
		{
			if(null== losePage)
			{
				losePage = new LosePage;
			}
			addChild(losePage);
			gamePage.myStageManager.Pause();
			exchangePrevPage(losePage);
		}
		
		public function systemHelp():void
		{
			if(helpPage==null)
			{
				helpPage = new HelpPage;
				
			}
			this.addChild(helpPage);
			exchangePrevPage(helpPage);
			removePrePage();
		}
		
		public function systemEnd():void
		{
			
		}
		
		public function systemAnimation():void
		{
			if(null== animationPage)
			{
				animationPage = new AnimationPage;
			}
			this.addChild(animationPage);
		//	gamePage.myStageManager.Pause();
			exchangePrevPage(animationPage);
			removePrePage();
			if("end" == animationPage.animation.currentFrameLabel)
			{
				switchSystemState(FrameWorkStates.STATE_STAGE_MODE);
			}
		}
		
		public function removePrePage():void
		{
			if(prevPage != null && prevPage.isRemain == false)
			{
				removeChild(prevPage);
				prevPage = null;
			}
		}		
		
		public function exchangePrevPage(page:BasicPage):BasicPage
		{
			if(currPage != page)
			{
				prevPage = currPage;
				currPage = page;
			}
			return page;
		}
		
	}
}